<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="受理编号" prop="bizNo">
        <el-input
          v-model="queryParams.bizNo"
          placeholder="请输入受理编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >批量导出</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="受理编号" align="center" prop="bizNo" />
      <el-table-column label="申请人" align="center" prop="applyUserName" />
      <el-table-column label="经办人" align="center" prop="operatorName" />
      <el-table-column label="公司名称" align="center" prop="vendorName" />
      <el-table-column label="申请事项" align="center" prop="applyReason" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="statusStr" />
      <el-table-column label="办理科室" align="center" prop="deptName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleAuth(scope.row, 'submit')"
            v-if="user.userId == scope.row.auditUserId && scope.row.type == 10"
            v-hasPermi="['collect:task:reviewSubmit']"
            >提交审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleViewAuth(scope.row, 'reviewer')"
            v-if="user.userId == scope.row.auditUserId && scope.row.type == 12"
            v-hasPermi="['collect:task:reviewReviewer']"
            >复核人审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleViewAuth(scope.row, 'department')"
            v-if="user.userId == scope.row.auditUserId && scope.row.type == 20"
            v-hasPermi="['collect:task:reviewDepartment']"
            >科室负责人审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            v-hasPermi="['collect:task:reviewLeader']"
            v-if="user.userId == scope.row.auditUserId && scope.row.type == 30"
            @click="handleViewAuth(scope.row, 'leader')"
            >分管领导审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            v-hasPermi="['collect:task:edit']"
            v-if="scope.row.status == 2"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left"
            v-hasPermi="['collect:task:withdraw']"
            v-if="scope.row.status == 2"
            @click="handleWithdraw(scope.row)"
            >撤回</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            v-hasPermi="['collect:task:completed']"
            v-if="scope.row.status == 2 || scope.row.status == 21"
            @click="handleCompleted(scope.row)"
            >办结</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
            v-if="
              (user.deptId == 204 || user.deptId == 207 || user.deptId == 208)  &&
              scope.row.status != '20' && scope.row.status != '4'
            "
            v-hasPermi="['collect:task:process']"
            >发起流程
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <Withdraw
      :row="row"
      @onBack="onBack"
      @onSuccess="onDeatilSuccess"
      v-if="isWithdraw"
    />
    <Completed
      :row="row"
      @onBack="onBack"
      @onSuccess="onDeatilSuccess"
      v-if="isCompleted"
    />
    <Detail
      :row="row"
      @onBack="onBack"
      @onSuccess="onDeatilSuccess"
      v-if="isDetail"
    />
    <Form
      :row="row"
      v-if="isEdit"
      @onBack="
        (flag) => {
          this.isEdit = false;
          if (flag) getList();
        }
      "
    />
    <Audit :row="row" @onBack="onBack" @onSuccess="onSuccess" v-if="isAudit" />
  </div>
</template>

<script>
import Withdraw from "./withdraw.vue";
import Completed from "./completed.vue";
import Audit from "./audit.vue";
import Detail from "./detail.vue";
import Form from "../collect/form.vue";
import { vendorAuditList } from "@/api/collect/task/index";
// import { listDept } from "@/api/system/dept";
import { storage } from "@/utils/util.js";
import { vendorAuditResponsibilityAudit } from "@/api/collect/task/index";
export default {
  components: {
    Withdraw,
    Completed,
    Detail,
    Form,
    Audit,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      isAudit: false,
      //行内容
      row: {},
      // 选中数组
      ids: [],
      //用户信息
      user: storage.getStorage("user") || {},
      //列表数据内容
      tableList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 是否撤回
      isWithdraw: false,
      // 是否办结
      isCompleted: false,
      // 是否显示详情
      isDetail: false,
      // 是否显示编辑
      isEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        acceptNo: undefined,
        productCompanyName: undefined,
      },
    };
  },
  created() {
    this.getList();
    //this.getDeptList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      vendorAuditList(this.queryParams).then((response) => {
        this.tableList = response.rows || [];
        this.total = response?.total || 0;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交表单 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
        }
      });
    },

    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    /** 提交审核 */
    handleAuth(row, authType) {
      row.authType = authType;
      this.row = row;
      this.isAudit = true;
    },

    /** 编辑 */
    handleEdit(row) {
      this.row = row;
      this.isEdit = true;
    },

    /** 撤回 */
    handleWithdraw(row) {
      this.row = row;
      this.isWithdraw = true;
    },
    /** 办结 */
    handleCompleted(row) {
      this.row = row;
      this.isCompleted = true;
    },

    /** 查看审核 */
    handleViewAuth(row, authType) {
      row.authType = authType;
      row.isAuth = true;
      this.row = row;
      this.isDetail = true;
    },

    /** 查看详情 */
    handleView(row) {
      row.isAuth = false;
      this.row = row;
      this.isDetail = true;
    },

    /** 发起流程 */
    handleProcess(row) {
      this.$router.push({ path: "/task/process", query: { id: row.id } });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "vendor/operator/audit/export",
        {
          ...this.queryParams,
        },
        `待办任务${new Date().getTime()}.xlsx`,
        {
          headers: { "Content-Type": "application/json;charset=utf-8" },
          isTansParams: true,
        }
      );
    },
    /** 关闭弹窗  */
    onBack(key) {
      this[key] = false;
    },
    /** 审核成功回调 */
    onSuccess(data) {
      let array = data.nextAudit.split("/");
      if (this.row.authType === "submit") {
        let params = {
          id: this.row.id,
          nextAuditId: array[0],
          nextAuditName: array[1],
          nextAuditType: data.nextAuditType ? data.nextAuditType.join(",") : "",
        };
        vendorAuditResponsibilityAudit(params).then(() => {
          this.Message("审核成功");
          this.isAudit = false;
          this.getList();
        });
        return;
      }
      this.getList();
    },
    /**  详情成功回调 */
    onDeatilSuccess(key) {
      this[key] = false;
      this.getList();
    },
  },
};
</script>
